﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormCurrentCurve : Form
    {
        /// <summary>
        /// 曲线名称列表
        /// </summary>
        private string[] CurveNames;

        /// <summary>
        /// 曲线颜色列表
        /// </summary>
        private Color[] CurveColors;

        /// <summary>
        /// 温度记录
        /// </summary>
        private List<CurrentRecordView> CurrentList;

        public FormCurrentCurve()
        {
            InitializeComponent();

            CurveNames = new string[Config.镀铜槽数];
            for(int i=0; i<Config.镀铜槽数; i++)
            {
                CurveNames[i] = string.Format("铜{0}", i+1);
            }

            CurveColors = new Color[]
            {
                Color.DodgerBlue,
                Color.FromArgb(99,178,238),
                Color.FromArgb(118,218,145),
                Color.FromArgb(248,203,127),
                Color.FromArgb(248,149,136),
                Color.FromArgb(124,214,207),
                Color.FromArgb(145,146,171),
                Color.FromArgb(120,152,225),
                Color.FromArgb(239,166,102),
                Color.FromArgb(237,221,134),
                Color.FromArgb(153,135,206),
            };

            CurrentList = new List<CurrentRecordView>();
        }

        private void FormCurrentCurve_Load(object sender, EventArgs e)
        {
            ComboBox_Slot.DataSource = CurveNames;

            DTP_End.Value = DateTime.Now.Date;
            DTP_Start.Value = DTP_End.Value.AddDays(-2);

            CurrentList = CurrentRecordView.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(CurrentList, ComboBox_Slot.SelectedIndex);
        }

        private void UpdateCurve(List<CurrentRecordView> list, int index)
        {
            HCH_Current.RemoveAllCurve();

            var array = (from iter in list orderby iter.Time ascending select iter).ToArray();
            var times = new DateTime[array.Length];
            var temp = new float[CurveNames.Length][][];
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = new float[4][];

                for(int j=0; j<4; j++)
                {
                    temp[i][j] = new float[array.Length];
                }
            }
            for (int i = 0; i < array.Length; i++)
            {
                times[i] = array[i].Time;

                int slots = array[i].SlotCurrent.Length;
                for(int slot=0; slot<slots; slot++)
                {
                    temp[slot][0][i] = (float)array[i].SlotCurrent[slot].A[0];
                    temp[slot][1][i] = (float)array[i].SlotCurrent[slot].A[1];
                    temp[slot][2][i] = (float)array[i].SlotCurrent[slot].B[0];
                    temp[slot][3][i] = (float)array[i].SlotCurrent[slot].B[1];
                }
            }

            HCH_Current.SetDateTimes(times);
            HCH_Current.SetLeftCurve(CurveNames[index] + "#A1", temp[index][0], CurveColors[1], HslControls.CurveStyle.LineSegment, "{0:F1} A");
            HCH_Current.SetLeftCurve(CurveNames[index] + "#A2", temp[index][1], CurveColors[2], HslControls.CurveStyle.LineSegment, "{0:F1} A");
            HCH_Current.SetLeftCurve(CurveNames[index] + "#B1", temp[index][2], CurveColors[3], HslControls.CurveStyle.LineSegment, "{0:F1} A");
            HCH_Current.SetLeftCurve(CurveNames[index] + "#B2", temp[index][3], CurveColors[4], HslControls.CurveStyle.LineSegment, "{0:F1} A");

            HCH_Current.RenderCurveUI();
        }

        private void btn退出_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            CurrentList = CurrentRecordView.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(CurrentList, ComboBox_Slot.SelectedIndex);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            CurrentList = CurrentRecordView.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(CurrentList, ComboBox_Slot.SelectedIndex);
        }

        private void ComboBox_Slot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ComboBox_Slot.SelectedIndex < 0) return;

            UpdateCurve(CurrentList, ComboBox_Slot.SelectedIndex);
        }
    }
}



    

       


    
    
   
    
    

